# 识别验证码
from PIL import Image
from PIL import ImageOps
import time,os,sys
import pytesseract
os.environ['TESSDATA_PREFIX'] = 'C:\\Program Files (x86)\\Tesseract-OCR\\tessdata'
def cleanImage(imagePath):
    image = Image.open(imagePath)   #打开图片
    image = image.point(lambda x: 0 if x<143 else 255)  #处理图片上的每个像素点，使图片上每个点“非黑即白”
    image = image.convert('1')
    borderImage = ImageOps.expand(image,border=1,fill='white')
    borderImage.save(imagePath)
basedir = os.path.abspath(os.path.dirname(__file__))
cleanImage(os.path.join(basedir, "vcode.jpg"))

out = Image.open(os.path.join(basedir, "vcode.jpg"))
text = pytesseract.image_to_string(out)
print("text:" + text)